package com.sgb.member.view_model;

import com.sgb.member.di.repository.MemberRepertory;
import com.sgb.member.entity.ListMResponse;
import com.sgb.member.entity.MemberPersonBean;
import com.sgb.member.ui.fragment.HomePersonMemberFragment;
import com.swgk.core.base.BaseViewModel;
import com.swgk.core.base.api.ICallBack;
import com.swgk.core.base.model.entity.BaseEntity;
import com.swgk.core.dialog.DialogHelper;
import com.swgk.core.view.selection.CommonPopEntity;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import retrofit2.Call;

public class HomePersonMemberModel extends BaseViewModel {

    private final HomePersonMemberFragment fragment;
    private final MemberRepertory mRepertory;
    private int current;

    private final CommonPopEntity mAll;

    public HomePersonMemberModel(HomePersonMemberFragment fragment, MemberRepertory repertory) {
        this.fragment = fragment;
        this.mRepertory = repertory;

        mAll = new CommonPopEntity();
        mAll.setNo("");
        mAll.setTypeName("全部");
    }

    private void showOrHideDialog(boolean aBoolean) {
        if (aBoolean) {
            if (!DialogHelper.isShowing() && fragment.getActivity() != null) {
                DialogHelper.showProgressDialog(fragment.getActivity(), "数据获取中...");
            }
        } else {
            DialogHelper.dismissProgressDialog();
            fragment.closeRecyclerRefreshing();
        }
    }

    // 会员类型二级
    public void getMemberTypeSubList(String no) {
        showOrHideDialog(true);
        mRepertory.getApi().getMemberTypeSubList(no).enqueue(new ICallBack<BaseEntity<List<CommonPopEntity>>>() {
            @Override
            public void onSuccess(BaseEntity<List<CommonPopEntity>> data) {
                if (data.getData() != null && data.getData().size() > 0) {
                    data.getData().add(0, mAll);
                    fragment.setSubTabLayoutData(data.getData());
                } else {
                    fragment.setSubTabLayoutData(new ArrayList<>());
                }
            }


            @Override
            public void onFailure(Call<BaseEntity<List<CommonPopEntity>>> call, Throwable t) {
                showOrHideDialog(false);
                fragment.setTabLayoutData(new ArrayList<>());
            }

            @Override
            public void onFinally() {

            }
        });
    }

    // 会员类型
    public void getMemberTypeList(int pos) {
        showOrHideDialog(true);
        // 0 供方 1需方
        mRepertory.getApi().getMemberTypeList(pos == 0 ? "1" : "0").enqueue(new ICallBack<BaseEntity<List<CommonPopEntity>>>() {
            @Override
            public void onSuccess(BaseEntity<List<CommonPopEntity>> data) {
                if (data.getData() != null && data.getData() != null && data.getData().size() > 0) {
                    data.getData().add(0, mAll);
                    fragment.setTabLayoutData(data.getData());
                } else {
                    fragment.setTabLayoutData(new ArrayList<>());
                }
            }
            @Override
            public void onFailure(Call<BaseEntity<List<CommonPopEntity>>> call, Throwable t) {
                showOrHideDialog(false);
                fragment.setTabLayoutData(new ArrayList<>());
            }

            @Override
            public void onFinally() {

            }
        });
    }

    // 需方会员
    public void getJoinMembers(boolean isLoadMore, String mNo1, String mNo11, String relation) {
        showOrHideDialog(true);
        int currentPage = isLoadMore ? current + 1 : 1;
        Map<String, Object> map = new HashMap<>();
        map.put("memberType", mNo1); //会员类型
        map.put("secondMemberType", mNo11); //会员类型二级
        map.put("relation", relation); // 会员关系 1供方 2需方
        int maxPageSize = 10;
        map.put("limit", maxPageSize);
        map.put("page", currentPage);
        mRepertory.getApi().getMemberLeagueCompanyList(map).enqueue(new ICallBack<BaseEntity<ListMResponse<List<MemberPersonBean>>>>() {
            @Override
            public void onSuccess(BaseEntity<ListMResponse<List<MemberPersonBean>>> data) {
                current = data.getData().getCurrent();
                int total = data.getData().getPages();
                fragment.refreshRecycler(data.getData().getList(), isLoadMore, current >= total);
            }

            @Override
            public void onFailure(Call<BaseEntity<ListMResponse<List<MemberPersonBean>>>> call, Throwable t) {
                super.onFailure(call, t);
                if (!isLoadMore) {
                    fragment.showEmpty();
                }
            }

            @Override
            public void onFinally() {
                showOrHideDialog(false);
            }
        });
    }

   /* // 供方会员
    public void getMyMembers(boolean isLoadMore, String companyName, String mNo1, String mNo11, String mNo2, String mNo3, String mNo4, String mNo41, String mNo5, String mNo6, String mNo7, String mNo8, String mNo9, String mNo10) {
        int currentPage = isLoadMore ? current + 1 : 1;
        showOrHideDialog(true);
        Map<String, Object> map = new HashMap<>();
        map.put("companyName", companyName); //搜索企业名称
        map.put("memberType", mNo1); //会员类型
        map.put("secondMemberType", mNo11); //会员类型二级
        map.put("companyType", mNo2); //企业类型
        map.put("level", mNo3); //企业等级
        map.put("provinceNo", mNo4); //经营地址 省【add】
        map.put("cityNo", mNo41); //经营地址 市【add】
        map.put("qualificationType", mNo5); //资质类型【add】
        map.put("honourType", mNo6); //荣誉分类【add】
        map.put("assetCategories", mNo7);//固定资产分类【add】
        map.put("knowledgeType", mNo8); //知识产权分类【add】
        map.put("moneyType", mNo9); //单次金额最大【add】
        map.put("isHaveZc", mNo10); //是否有招采信息【add】
        map.put("limit", maxPageSize);
        map.put("page", currentPage);
        mRepertory.getApi().getMembers(map).enqueue(new ICallBack<BaseEntity<ListMResponse<List<MemberBean>>>>() {
            @Override
            public void onSuccess(BaseEntity<ListMResponse<List<MemberBean>>> data) {
                current = data.getData().getCurrent();
                int total = data.getData().getPages();
                fragment.refreshRecycler(data.getData().getList(), isLoadMore, current >= total);
            }

            @Override
            public void onFailure(Call<BaseEntity<ListMResponse<List<MemberBean>>>> call, Throwable t) {
                super.onFailure(call, t);
                if (!isLoadMore) {
                    fragment.showEmpty();
                }
            }

            @Override
            public void onFinally() {
                showOrHideDialog(false);
            }
        });
    }*/
}
